package com.beauty.dressing.module.market.presenter;

import com.beauty.dressing.common.base.BasePresenter;
import com.beauty.dressing.common.bean.BaseObserver;
import com.beauty.dressing.common.bean.BaseParse;
import com.beauty.dressing.common.bean.ExtraParser;
import com.beauty.dressing.common.bean.SalesOrderResp;
import com.beauty.dressing.common.bean.ShopCart;
import com.beauty.dressing.module.market.presenter.contract.PayContract;
import com.beauty.dressing.module.market.presenter.contract.ShopCartContract;
import com.beauty.dressing.rxandroid.api.ApiManager;

import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/04/11
 */

public class ShopCartPresenter extends BasePresenter implements ShopCartContract.Presenter {

    private ShopCartContract.ViewControl mViewControl;

    public ShopCartPresenter(ShopCartContract.ViewControl vcl) {
        this.mViewControl = vcl;
    }

    @Override
    public void getShopcartList(int page) {
        Map<String,String> map = httpParams.getHeaderMap();
        map.put("page",String.valueOf(page));
        ApiManager.getInstence().getOrderServie().getShopcarList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object,ShopCart>() {

                    @Override
                    protected void onSuccees(String message, Object data, List<ShopCart> list, BaseParse extra) {
                        if (isDestoryed) return;
                        int currentPage = 0;
                        if (extra instanceof ExtraParser){
                            currentPage = ((ExtraParser)extra).current_page_index;
                        }
                        mViewControl.onShopcartList(true, 200, message, list,currentPage);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data,List<ShopCart> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onShopcartList(false, code, message, list,0);
                    }
                });
    }

    @Override
    public void deleteShopcart(final long cartId) {
        Map<String,String> map = httpParams.getHeaderMap();
        map.put("id",String.valueOf(cartId));
        ApiManager.getInstence().getOrderServie().deleteShopcar(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Object,Object>() {

                    @Override
                    protected void onSuccees(String message, Object data, List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onDeleteShopcart(true, 200, message, cartId);
                    }

                    @Override
                    protected void onFailed(int code, String message, Object data,List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onDeleteShopcart(false, code, message, cartId);
                    }
                });
    }

    @Override
    public void createOrder(long lease_id, int number) {
        Map<String,String> map = httpParams.getHeaderMap();
        map.put("lease_id",String.valueOf(lease_id));
        map.put("number",String.valueOf(number));
        ApiManager.getInstence().getOrderServie().createSalesOrderFromCart(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<SalesOrderResp,Object>() {

                    @Override
                    protected void onSuccees(String message, SalesOrderResp data, List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onCreateOrder(true, 200, message, data);
                    }

                    @Override
                    protected void onFailed(int code, String message, SalesOrderResp data,List<Object> list, BaseParse extra) {
                        if (isDestoryed) return;
                        mViewControl.onCreateOrder(false, code, message, data);
                    }
                });
    }
}
